package com.uniknow.app.util;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;

import com.uniknow.app.AppConfig;
import com.uniknow.app.AppContext;

/**
 * 切换table的动画效果，兼容2.2,翻转特效仅支持3.0及以上
 * @author lianjiao
 *
 */
public class FlipsView {
	private static Interpolator accelerator = new AccelerateInterpolator();
	private static Interpolator decelerator = new DecelerateInterpolator();
	@SuppressLint("NewApi")
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public static void ChangeTable(View fromView,View toView,Object endAnimator) {
			if(AppContext.isMethodsCompat(android.os.Build.VERSION_CODES.HONEYCOMB)){
					 final View visibleList;
				     final View invisibleList;
				     if (fromView.getVisibility() == View.GONE) {
				         visibleList = toView;
				         invisibleList = fromView;
				     } else {
				         invisibleList = toView;
				         visibleList = fromView;
				     }
				     ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visibleList, "rotationY", 0f, 90f);
				     visToInvis.setDuration(300);
				     if(!AppConfig.isAnimation)visToInvis.setDuration(0);
				     visToInvis.setInterpolator(accelerator);
				     final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisibleList, "rotationY",
				             -90f, 0f);
				     invisToVis.setDuration(300);
				     if(!AppConfig.isAnimation)invisToVis.setDuration(0);
				     invisToVis.setInterpolator(decelerator);
				     visToInvis.addListener(new AnimatorListenerAdapter() {
				         @TargetApi(Build.VERSION_CODES.HONEYCOMB)
						@Override
				         public void onAnimationEnd(Animator anim) {
				             visibleList.setVisibility(View.GONE);
				             invisToVis.start();
				             invisibleList.setVisibility(View.VISIBLE);
				         }
				     });
				     visToInvis.start();
				     invisToVis.addListener((AnimatorListener) endAnimator);
				 
			}else{
				fromView.setVisibility(View.GONE);
				toView.setVisibility(View.VISIBLE);
			}
	 }
	
	public static void ChangeMainTable(View fromView,View toView,Object endAnimator){
		
	}
	
}
